package com.youmeek.ssm.module.user.controller;

import com.youmeek.ssm.module.user.pojo.Page;
import com.youmeek.ssm.module.user.pojo.Supplier;
import com.youmeek.ssm.module.user.service.impl.SupplierService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 作者：13687
 * 日期：2017/5/22
 * 项目：ssm
 * 描述：供应商控制器
 */
@Controller
@RequestMapping("/supplier")
public class SupplierController {

	@Resource
	private	SupplierService supplierService;

	@RequestMapping("/supplierlist")
	public String supplierlist(){
		return "supplierlist";
	}


	// 如果返回json格式，需要这个注解
	// page:1
	// rows:5
/*	@RequestMapping("/selectPage")
	@ResponseBody
	public Object selectPage(HttpServletRequest request){
		String index = request.getParameter("page");
        String pageSize = request.getParameter("rows");
        String keyWord = request.getParameter("keyWord");

        Page<Supplier> page = new Page<>();
        page.keyWord = keyWord;
        page.index = Integer.valueOf(index);
        page.pageSize = Integer.valueOf(pageSize);

		Page<Supplier> origin = supplierService.selectPage(page);

		Map<String,Object> result = new HashMap<>();
		result.put("total",origin.total);
		result.put("rows",origin.getList());
		return result;
	}*/

	@RequestMapping("/selectPage")
	@ResponseBody
	public Object selectPage(Page<Supplier> reqp,Supplier supplier){
//	public Object selectPage(int page,int rows,String keyWord){
//		String index =    request.getParameter("page");
//		String pageSize = request.getParameter("rows");
//		String keyWord =  request.getParameter("keyWord");

//		Page<Supplier> pages = new Page<>();
//		pages.rows = rows;
//		pages.page = page;
//		pages.keyWord = keyWord;
        /**
         * 请求参数分为两部分：
         * 1. 分页 reqp
         * 2. 查询参数 supplier
         * */
		reqp.setParamsEntity(supplier);
		Page<Supplier> origin = supplierService.selectPage(reqp);

		Map<String,Object> result = new HashMap<>();
		result.put("total",origin.getTotal());
		result.put("rows",origin.getList());
		return result;
	}

	@RequestMapping("/deleteByPks")
	@ResponseBody
	public int deleteByPks(String[] pks){
		System.out.println("---deleteByPks.pks:" + pks);
		int i = 0;
		try {
			i = supplierService.deleteByPks(pks);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}

	@RequestMapping("/insert")
	public String insertSupplier(){
		return "insert";// 返回新增页面
	}


	@RequestMapping("/updata")
	public String updataSupplier(){
		return "updata";
	}

	@RequestMapping("/insertSup")
	@ResponseBody
	public Object insertSup(Supplier supplier){
		System.out.println("提交的参数："+supplier.toString());
		int i=0;
		try {
			i = supplierService.insert(supplier);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}

	@RequestMapping("/updataSup")
	@ResponseBody
	public Object updata(Supplier supplier){
		int i = 0;
		try {
			i=supplierService.update(supplier);
		}catch (Exception e){
			e.printStackTrace();
		}
		return i;
	}


}
